/**
 * Created by oneminute on 7/17/14.
 */

var lz = require("../lib/utils/modules")();

exports.post = function (_req, _res, _opts, _callback) {
    var user = _req.user;
    if (!user) {
        lz.Logger.warn("/owngoods: 用户为临时用户。");
        _callback(lz.Config.errors.getError(lz.Config.errors.REQUEST_TEMPUSER_ERROR));
        return;
    }
    var description = _req.body.description;
    var did = _req.query.nid;
    var demand = new lz.Demand({id: did});
    demand.load({method: "byId"}, function (_err, _demand) {
        if (_err) {
            lz.Logger.error("/updateneed: 更新需求出现错误。");
            _callback(_err, user);
            return;
        }

        if (!_demand) {
            lz.Logger.error("/updateneed: 没有找到需求。");
            _callback(lz.Config.errors.getError(lz.Config.errors.REQUEST_NO_DEMAND));
            return;
        }
        demand.description = description;
        demand.save(function (_err, _demand) {
            if (_demand) {
                lz.Logger.debug("更新需求成功。");
                _callback(null, user);
            } else {
                lz.Logger.warn("更新需求失败。");
                _callback(_err, user);
            }
        });
    });
};
